package com.beiding.listener;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent;

public abstract class AbstractMongoDBListener<T> extends AbstractMongoEventListener<T> {


    @Override
    public void onBeforeConvert(BeforeConvertEvent<T> event) {
        onSave(event.getSource());
    }

    @Override
    public void onBeforeDelete(BeforeDeleteEvent<T> event) {
        ObjectId objectId = event.getSource().get("_id",ObjectId.class);
        onDelete(objectId.toString());
    }

    protected abstract void onSave(T obj);

    protected abstract void onDelete(String id);


}
